package com.getword.web.model;

import java.io.Serializable;
import java.util.Map;


/**
 * code=0  --成功  msg=''
 */
public class Result implements Serializable {
    private static final long serialVersionUID = -999362211833007653L;
    /**
     * 是否成功，成功找data、失败找msg
     */
    private boolean success;

    /**
     * 返回结果编码，成功的话我喜欢设为0
     */
    private int code;

    /**
     * 返回消息，一般放置可追溯的错误消息
     */
    private String msg;


    /**
     * 返回数据
     */
    private final Object data;

    /**
     * 额外参数,不使用Map<String, Object>防止JSON解析时过于复杂
     */
    private Map<String, String> extraInfo;

    private Result(boolean success, int code, String msg, Object data, Map<String, String> extraInfo) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.extraInfo = extraInfo;
    }
//    private Result(String msg, Object data) {
//        this.msg = msg;
//        this.data = data;
//    }

    public static Result fail(int code, String msg, Map<String, String> extraInfo) {
        return new Result(false, code, msg, null, extraInfo);
    }
    // status=0 表示失败
    public static Result fail(String msg) {
        return new Result(false, 1, msg, null, null);
    }
    public static Result fail(String msg, Object data) {
        return new Result(false, 1, msg, data, null);
    }

    public static Result fail(ResultCode resultCode, Map<String, String> extraInfo) {
        return new Result(false, resultCode.getCode(), resultCode.getMsg(), null, extraInfo);
    }

    public static Result fail(ResultCode resultCode,String msg) {
        return new Result(false, resultCode.getCode(), resultCode.getMsg(), null, null);
    }

    public static Result success(Object data, Map<String, String> extraInfo) {
        return new Result(true, 0, null, data, extraInfo);
    }

    public static Result success(ResultCode resultCode, Object data, Map<String, String> extraInfo) {
        return new Result(true, resultCode.getCode(), resultCode.getMsg(), data, extraInfo);
    }

    // 使用
    public static Result success(Object data) {
        return new Result(true, 0, "", data, null);
    }



    public boolean isSuccess() {
        return success;
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public Object getData() {
        return data;
    }

    public Map<String, String> getExtraInfo() {
        return extraInfo;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}